home *** CD-ROM | disk | FTP | other *** search
- /*
- Example.c
-
- Example extension. Demonstrates use of PatchWorks trap patching
- system to patch MenuSelect. Uses new GenericPatch class.
-
- by Mouse Herrell & Patrick Beard.
-
- © 1991 Berkeley Systems Inc.
- */
-
- #include <string.h>
- #include <Notification.h>
- #include <Traps.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stddef.h>
-
- #include <Exceptions.h>
- #include <Extension.h>
- #include <GenericPatch.h>
-
- static int dprintf(const char* format, ...);
-
- //
- // MenuSelectPatch -- Example of a head-off patch.
- //
-
- class MenuSelectPatch : public GenericPatch {
- public:
- MenuSelectPatch();
- virtual void Behavior(void);
-
- private:
- long itsCallCount;
- };
-
- struct MenuSelectParameters {
- Point itsStartPt;
- long itsResult;
- };
-
- typedef struct MenuSelectParameters MenuSelectParameters;
-
- typedef pascal long (*MenuSelectProcPtr) (Point pt);
-
- MenuSelectPatch::MenuSelectPatch()
- {
- // initialize instance variables.
- itsCallCount = 0;
-
- // install the appropriate patch.
- GenericPatch::InitGenericPatch(_MenuSelect, offsetof(MenuSelectParameters, itsResult));
- Install();
- }
-
- void MenuSelectPatch::Behavior()
- {
- KeyMap Keys;
-
- // announce our presence if control key held down.
- ++itsCallCount;
- GetKeys(Keys);
- if (Keys[1] & 0x8) {
- MenuSelectParameters* params = (MenuSelectParameters*)itsFrame->parameters;
-
- // head off the patch and return.
- params->itsResult = 0;
- AbortTrap();
-
- // print out where we are so far.
- dprintf("MenuSelect: startPt = (%d, %d), %ld calls.",
- params->itsStartPt, itsCallCount);
- }
- }
-
- //
- // Install routine. This is where you allocate your patch objects.
- // Throw an exception if something fails.
- //
-
- void Install()
- {
- try {
- // if extension succeeds, show happy icon.
- new MenuSelectPatch;
- ShowIconFamily(128);
- } catch {
- // extension failed, show sad icon.
- ShowIconFamily(129);
- throw(theException);
- }
- }
-
- //
- // Remove routine. This is called when system is shutdown.
- // Perhaps this should be removed. Rob thinks it shouldn't even be
- // part of the design.
- //
-
- void Remove()
- {
- Patch::RemoveAll();
- }
-
- //
- // debugging printf.
- //
-
- static int dprintf(const char* format, ...)
- {
- va_list args;
- static char str[256];
- int count;
-
- va_start(args, format);
- count = vsprintf(str, format, args);
- va_end(args);
- DebugStr(c2pstr(str));
-
- return count;
- }
-